package mysql

import (
	"doctor-medicine-system/docrpc/docrpc"

	"gorm.io/gorm"
)

// 药品drug
type Drug struct {
	gorm.Model
	Norms      string `gorm:"type:varchar(255);not null;comment:'规格'"`   //规格
	Vendor     string `gorm:"type:varchar(255);not null;comment:'生产厂商'"` //生产厂商
	Indication string `gorm:"type:varchar(255);not null;comment:'适应症'"`  //适应症
	Dosage     string `gorm:"type:varchar(255);not null;comment:'用法用量'"` //用法用量
	Matter     string `gorm:"type:varchar(255);not null;comment:'事项'"`   //事项
	DocTocId   int64  `gorm:"type:int(11);not null;comment:'医生id'"`      //医生id
}

func (d *Drug) DrugAdd(db *gorm.DB) error {
	return db.Debug().Create(d).Error
}

func (d *Drug) WhereId(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).First(d).Error
}

func (d *Drug) FIndDrugID(db *gorm.DB, id int64) interface{} {
	return db.Debug().Where("id=?", id).First(d).Error
}

func (d *Drug) Update(db *gorm.DB) error {
	return db.Debug().Updates(d).Error
}

func DrugList(db *gorm.DB) []*docrpc.DrugList {
	var list []*docrpc.DrugList
	db.Debug().Model(&Drug{}).Select("drugs.id as durg_id,drugs.norms,drugs.vendor,drugs.indication,drugs.dosage,drugs.matter,doc_tocs.id as doctoc_id,doc_tocs.doc_toc_code,doc_tocs.password").
		Joins("LEFT JOIN doc_tocs ON drugs.doc_toc_id = doc_tocs.id").Find(&list)
	return list
}
